package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.service.AdminCategoryService;
import com.cskaoyan.market.service.AdminCategoryServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.L1Vo;
import com.cskaoyan.market.vo.MarketCategoryVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/admin/category/*")
public class AdminCategoryController extends HttpServlet {

    AdminCategoryService adminCategoryService =new AdminCategoryServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/category/", "");
        if("list".equals(op)){
            list(req,resp);
        }
        if("l1".equals(op)){
            l1(req,resp);
        }
    }

    private void l1(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<MarketCategory> marketCategories = adminCategoryService.responseL1();
        List<L1Vo> l1Vos = new ArrayList<>();
        for (MarketCategory marketCategory : marketCategories) {
            L1Vo l1Vo = new L1Vo(marketCategory.getId(),marketCategory.getName());
            l1Vos.add(l1Vo);
        }

        Object okList = ResponseUtil.okList(l1Vos);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(okList);
        resp.getWriter().write(s);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            List<MarketCategoryVo> resultList=adminCategoryService.list();
        Object okList = ResponseUtil.okList(resultList);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(okList);
        resp.getWriter().write(s);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/category/", "");
        if("create".equals(op)){
            create(req,resp);
        }
        if("update".equals(op)){
            update(req,resp);
        }
        if("delete".equals(op)){
            delete(req,resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        MarketCategoryVo categoryVo = JacksonUtil.getObjectMapper().readValue(requestBody, MarketCategoryVo.class);
        Integer deletePid=null;
        if("L1".equals(categoryVo.getLevel())){
            deletePid=categoryVo.getId();
        }
        adminCategoryService.delete(categoryVo.getId(),deletePid);
        Object ok = ResponseUtil.ok();
        resp.getWriter().write(JacksonUtil.getObjectMapper().writeValueAsString(ok));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        MarketCategoryVo categoryVo = JacksonUtil.getObjectMapper().readValue(requestBody, MarketCategoryVo.class);
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setName(categoryVo.getName());
        marketCategory.setUpdateTime(LocalDateTime.now());
        marketCategory.setDesc(categoryVo.getDesc());
        marketCategory.setPid(categoryVo.getPid());
        marketCategory.setIconUrl(categoryVo.getIconUrl());
        marketCategory.setKeywords(categoryVo.getKeywords());
        marketCategory.setLevel(categoryVo.getLevel());
        Integer setId = categoryVo.getId();
        adminCategoryService.update(marketCategory,setId);

        Object ok = ResponseUtil.ok();
        resp.getWriter().write(JacksonUtil.getObjectMapper().writeValueAsString(ok));

    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String requstBody = req.getReader().readLine();
        MarketCategory marketCategory = JacksonUtil.getObjectMapper().readValue(requstBody, MarketCategory.class);

        adminCategoryService.create(marketCategory);
        Object ok = ResponseUtil.ok(marketCategory);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().write(s);
    }
}
